| Conditions | 1 |
| Paths | 4 |
| Total Lines | 81 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 31 | $.fn.symphonyPickable = function(options) { |
||
| 32 | var objects = $(this), |
||
| 33 | settings = { |
||
| 34 | content: '#contents', |
||
| 35 | pickables: '.pickable' |
||
| 36 | }; |
||
| 37 | |||
| 38 | $.extend(settings, options); |
||
| 39 | |||
| 40 | /*------------------------------------------------------------------------- |
||
| 41 | Events |
||
| 42 | -------------------------------------------------------------------------*/ |
||
| 43 | |||
| 44 | // Switch content |
||
| 45 | objects.on('change.pickable', function pick(event) { |
||
| 46 | var object = $(this), |
||
| 47 | choice = object.val(), |
||
| 48 | relation = object.attr('id') || object.attr('name'), |
||
| 49 | related = $(settings.pickables + '[data-relation="' + relation + '"]'), |
||
| 50 | picked, request; |
||
| 51 | |||
| 52 | // Handle numeric values |
||
| 53 | if($.isNumeric(choice) === true) { |
||
| 54 | choice = 'choice' + choice; |
||
| 55 | } |
||
| 56 | |||
| 57 | // Hide all choices |
||
| 58 | object.trigger('pickstart.pickable'); |
||
| 59 | related.hide().find('input, select, textarea').prop('readonly', true); |
||
| 60 | |||
| 61 | // Selection found |
||
| 62 | picked = $('#' + choice); |
||
| 63 | if(picked.length > 0) { |
||
| 64 | picked.show().find('input, select, textarea').prop('readonly', false).trigger('pick.pickable'); |
||
| 65 | object.trigger('pickstop.pickable'); |
||
| 66 | } |
||
| 67 | |||
| 68 | // Selection not found |
||
| 69 | else { |
||
| 70 | request = object.data('request'); |
||
| 71 | |||
| 72 | // Fetch picked element |
||
| 73 | if(request) { |
||
| 74 | $.ajax({ |
||
| 75 | type: 'GET', |
||
| 76 | url: request, |
||
| 77 | data: { 'choice': choice }, |
||
| 78 | dataType: 'html', |
||
| 79 | success: function(remote) { |
||
| 80 | content.append(remote); |
||
| 81 | remote.trigger('pick.pickable'); |
||
| 82 | object.trigger('pickstop.pickable'); |
||
| 83 | } |
||
| 84 | }); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | }); |
||
| 88 | |||
| 89 | /*------------------------------------------------------------------------- |
||
| 90 | Initialisation |
||
| 91 | -------------------------------------------------------------------------*/ |
||
| 92 | |||
| 93 | var content = $(settings.content); |
||
| 94 | |||
| 95 | // Set up relationships |
||
| 96 | objects.each(function init() { |
||
| 97 | var object = $(this), |
||
| 98 | relation = object.attr('id') || object.attr('name'); |
||
| 99 | |||
| 100 | object.find('option').each(function() { |
||
| 101 | $('#' + $(this).val()).attr('data-relation', relation); |
||
| 102 | }); |
||
| 103 | }); |
||
| 104 | |||
| 105 | // Show picked content |
||
| 106 | objects.trigger('change.pickable'); |
||
| 107 | |||
| 108 | /*-----------------------------------------------------------------------*/ |
||
| 109 | |||
| 110 | return objects; |
||
| 111 | }; |
||
| 112 | |||
| 114 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.